/**
 * 
 */
package No201_300.No221_230.ImplementStackusingQueues_225;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author 李敬
 *
 */
public class MyStack {
	Queue<Integer> queue = new LinkedList<>();
	Queue<Integer> q1 = new LinkedList<>();
	
    // Push element x onto stack.
    public void push(int x) {
        queue.offer(x);
    }

    // Removes the element on top of the stack.
    public void pop() {
    	while (this.queue.size()>1) {
			q1.offer(this.queue.poll());
		}
    	this.queue.poll();
    	Queue<Integer> tmp = q1;  
        q1 = this.queue;  
        this.queue = tmp;
    }

    // Get the top element.
    public int top() {
    	while (this.queue.size()>1) {
			q1.offer(this.queue.poll());
		}
    	int top = this.queue.peek();
    	q1.offer(this.queue.poll());
    	Queue<Integer> tmp = q1;  
        q1 = this.queue;  
        this.queue = tmp;  
    	
    	return top;
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return queue.isEmpty();
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
